FrameLib  0.7
DSP processing with frames of arbitrary timing and length
FrameLib_DSP.h
Go to the documentation of this file.
1 
2 #ifndef FRAMELIB_DSP_H
3 #define FRAMELIB_DSP_H
4 
5 #include "FrameLib_Types.h"
6 #include "FrameLib_Context.h"
7 #include "FrameLib_Object.h"
9 #include <limits>
10 #include <vector>
11 #include <algorithm>
12 
13 // FrameLib_DSP
14 
15 // This abstract class is the core of the DSP processing system and handles low level single channel connections and timing
16 
17 class FrameLib_DSP : public FrameLib_Block, public FrameLib_Queueable<FrameLib_DSP>
18 {
19  // Type definition for concision / Queue access
20 
22  typedef FrameLib_Queueable<FrameLib_DSP>::Queue LocalQueue;
24  friend class FrameLib_ProcessingQueue;
25 
26 protected:
27 
29  {
31  : mTimeAdvance(0), mNewFrame(false), mOutputDone(false) {}
32 
33  SchedulerInfo(FrameLib_TimeFormat timeAdvance, bool newFrame, bool outputDone)
34  : mTimeAdvance(timeAdvance), mNewFrame(newFrame), mOutputDone(outputDone) {}
35 
37  bool mNewFrame;
39  };
40 
41 private:
42 
43  struct Input
44  {
45  Input() : mObject(NULL), mIndex(0), mSize(0), mFixedInput(NULL), mType(kFrameNormal), mUpdate(false), mParameters(false), mTrigger(true), mSwitchable(false) {}
46 
47  // Connection Info
48 
49  FrameLib_DSP *mObject;
50  unsigned long mIndex;
51 
52  // Fixed Input
53 
54  unsigned long mSize;
55  double *mFixedInput;
56 
57  // Flags
58 
59  FrameType mType;
60 
61  bool mUpdate;
62  bool mParameters;
63  bool mTrigger;
64  bool mSwitchable;
65  };
66 
67  struct Output
68  {
69  Output() : mMemory(NULL), mType(kFrameNormal), mCurrentSize(0), mRequestedSize(0), mPointerOffset(0) {}
70 
71  void *mMemory;
72 
73  FrameType mType;
74 
75  size_t mCurrentSize;
76  size_t mRequestedSize;
77  size_t mPointerOffset;
78  };
79 
80 public:
81 
82  // Constructor / Destructor
83 
84  FrameLib_DSP(ObjectType type, FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns, unsigned long nOuts, unsigned long nAudioChans = 0);
85  ~FrameLib_DSP();
86 
87  // Set Fixed Inputs
88 
89  virtual void setFixedInput(unsigned long idx, double *input, unsigned long size);
90 
91  // Audio Processing
92 
93  virtual void blockUpdate(double **ins, double **outs, unsigned long blockSize);
94  virtual void reset(double samplingRate, unsigned long maxBlockSize);
95 
96  // Info (individual objects should override other methods to provide info)
97 
98  virtual const FrameLib_Parameters *getParameters() const { return &mParameters; }
99 
100  virtual FrameType inputType(unsigned long idx) const { return mInputs[idx].mType; }
101  virtual FrameType outputType(unsigned long idx) const { return mOutputs[idx].mType; }
102 
103  // Automatic Oredering Connections
104 
105  virtual void autoOrderingConnections();
106 
107 protected:
108 
109  // Setup and IO Modes
110 
111  // Call these from your constructor only (unsafe elsewhere)
112 
113  void setIO(unsigned long nIns, unsigned long nOuts, unsigned long nAudioChans = 0);
114  void inputMode(unsigned long idx, bool update, bool trigger, bool switchable, FrameType type = kFrameNormal);
115  void setParameterInput(unsigned long idx);
116  void addParameterInput();
117  void outputMode(unsigned long idx, FrameType type);
118 
119  // You should only call this from your update method (it is unsafe anywhere else)
120 
121  void updateTrigger(unsigned long idx, bool trigger);
122 
123  // Processing Utilities
124 
125  // Test if an Input Triggered the Current Frame
126 
127  bool isTrigger(unsigned long idx) const { return mInputs[idx].mTrigger && mInputs[idx].mObject && (mInputs[idx].mObject->mFrameTime == mFrameTime); }
128 
129  // Timing
130 
131  FrameLib_TimeFormat getFrameTime() const { return mFrameTime; }
132  FrameLib_TimeFormat getValidTime() const { return mValidTime; }
133  FrameLib_TimeFormat getInputTime() const { return mInputTime; }
134  FrameLib_TimeFormat getCurrentTime() const { return getType() == kScheduler ? mValidTime : mFrameTime; }
135  FrameLib_TimeFormat getBlockStartTime() const { return getType() == kOutput ? mBlockEndTime : mBlockStartTime; }
136  FrameLib_TimeFormat getBlockEndTime() const { return mBlockEndTime; }
137 
138  FrameLib_TimeFormat getInputFrameTime(unsigned long idx) const { return mInputs[idx].mObject ? mInputs[idx].mObject->mFrameTime : FrameLib_TimeFormat(0); }
139  FrameLib_TimeFormat getInputValidTime(unsigned long idx) const { return mInputs[idx].mObject ? mInputs[idx].mObject->mValidTime : FrameLib_TimeFormat(0); }
140 
141  // Output Allocation
142 
143  void requestOutputSize(unsigned long idx, size_t size) { mOutputs[idx].mRequestedSize = size; }
144  bool allocateOutputs();
145 
146  // Get Inputs and Outputs
147 
148  double *getInput(unsigned long idx, size_t *size);
149  FrameLib_Parameters::Serial *getInput(unsigned long idx);
150 
151  double *getOutput(unsigned long idx, size_t *size);
152  FrameLib_Parameters::Serial *getOutput(unsigned long idx);
153 
154  // Convience methods for copying and zeroing
155 
156  static void copyVector(double *output, double *input, unsigned long size) { std::copy(input, input + size, output); }
157  static void zeroVector(double *output, unsigned long size) { std::fill_n(output, size, 0.0); }
158 
159  // Get DSP Object for a Given Input/Output
160 
161  unsigned long getNumInputObjects(unsigned long blockIdx) { return 1; }
162  FrameLib_DSP *getInputObject(unsigned long blockIdx, unsigned long idx) { return this; }
163  virtual unsigned long getInputObjectIdx(unsigned long blockIdx, unsigned long idx) { return blockIdx; }
164 
165  FrameLib_DSP *getOutputObject(unsigned long blockIdx) { return this; }
166  virtual unsigned long getOutputObjectIdx(unsigned long blockIdx) { return blockIdx; }
167 
168  unsigned long getNumOrderingConnectionObjects() { return 1; }
169  FrameLib_DSP *getOrderingConnectionObject(unsigned long idx) { return this; }
170 
171 private:
172 
173  // Deleted
174 
175  FrameLib_DSP(const FrameLib_DSP&);
176  FrameLib_DSP& operator=(const FrameLib_DSP&);
177 
178  // Queueable Reset
179 
180  void reset(LocalQueue *queue);
181 
182  // Customisable Processing
183 
184  // Override to handle audio at the block level
185 
186  virtual void blockProcess(double **ins, double **outs, unsigned long blockSize) {}
187 
188  // Override to get called on audio reset
189 
190  virtual void objectReset() {}
191 
192  // Override for updates prior to schedule / process (e.g. adjusting triggers)
193 
194  virtual void update() {}
195 
196  // Override for scheduling code (scheduler objects must override this)
197 
198  virtual SchedulerInfo schedule(bool newFrame, bool noAdvance) = 0;
199 
200  // Override for main frame processing code (processor objects must override this)
201 
202  virtual void process() = 0;
203 
204  // Scheduling
205 
206  // This returns true if the object requires notification from an audio thread (is a scheduler/has audio input)
207 
208  bool requiresAudioNotification() { return getType() == kScheduler || getNumAudioIns(); }
209 
210  // Manage Output Memory
211 
212  inline void freeOutputMemory();
213  inline void releaseOutputMemory();
214 
215  // Dependency Notification
216 
217  inline void dependencyNotify(bool releaseMemory, bool fromInput);
218  void dependenciesReady();
219  void setOutputDependencyCount();
220  void incrementInputDependency();
221 
222  // Connections
223 
224  virtual void connectionUpdate(Queue *queue);
225  void addOutputDependency(FrameLib_DSP *object);
226  virtual void autoOrderingConnections(LocalQueue *queue);
227  virtual void clearAutoOrderingConnections();
228 
229 protected:
230 
231  // Member Variables
232 
233  // Sampling Rate and Maximum Block Size
234 
236  unsigned long mMaxBlockSize;
237 
238  // Parameters
239 
241 
242 private:
243 
244  // Processing Queue
245 
246  FrameLib_Context::ProcessingQueue mProcessingQueue;
247  FrameLib_DSP *mNext;
248 
249  // IO Info
250 
251  std::vector<FrameLib_DSP *> mInputDependencies;
252  std::vector<FrameLib_DSP *> mOutputDependencies;
253 
254  std::vector <Input> mInputs;
255  std::vector <Output> mOutputs;
256 
257  // Dependency Counts
258 
259  long mInputCount;
260  long mDependencyCount;
261  long mOutputMemoryCount;
262 
263  // Frame and Block Timings
264 
265  FrameLib_TimeFormat mFrameTime;
266  FrameLib_TimeFormat mValidTime;
267  FrameLib_TimeFormat mInputTime;
268  FrameLib_TimeFormat mBlockStartTime;
269  FrameLib_TimeFormat mBlockEndTime;
270 
271  bool mUpdatingInputs;
272  bool mNoLiveInputs;
273  bool mInUpdate;
274  bool mOutputDone;
275 };
276 
277 // ************************************************************************************** //
278 
279 // FrameLib_Processor - Simple class for process type objects (can't handle audio)
280 
282 {
283 
284 public:
285 
286  FrameLib_Processor(FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns = 0, unsigned long nOuts = 0)
287  : FrameLib_DSP(kProcessor, context, owner, info, nIns, nOuts) {}
288 
289  static ObjectType getType() { return kProcessor; }
290  static bool handlesAudio() { return false; }
291 
292 protected:
293 
294  // This prevents the user from needing to implement this method - doing so will do nothing
295 
296  virtual SchedulerInfo schedule(bool newFrame, bool noAdvance) { return SchedulerInfo(); }
297 
298  void setIO(unsigned long nIns, unsigned long nOuts) { FrameLib_DSP::setIO(nIns, nOuts); }
299 };
300 
301 // ************************************************************************************** //
302 
303 // FrameLib_AudioInput - Simple class for process type objects (can handle audio input)
304 
306 {
307 
308 public:
309 
310  FrameLib_AudioInput(FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns = 0, unsigned long nOuts = 0, unsigned long nAudioIns = 0)
311  : FrameLib_DSP(kProcessor, context, owner, info, nIns, nOuts, nAudioIns) {}
312 
313  static ObjectType getType() { return kProcessor; }
314  static bool handlesAudio() { return true; }
315 
316 protected:
317 
318  // This prevents the user from needing to implement this method - doing so will do nothing
319 
320  virtual SchedulerInfo schedule(bool newFrame, bool noAdvance) { return SchedulerInfo(); }
321 };
322 
323 // ************************************************************************************** //
324 
325 // FrameLib_AudioOutput - Simple class for process type objects (can handle audio output)
326 
328 {
329 
330 public:
331 
332  FrameLib_AudioOutput(FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns = 0, unsigned long nOuts = 0, unsigned long nAudioOuts = 0)
333  : FrameLib_DSP(kOutput, context, owner, info, nIns, nOuts, nAudioOuts) {}
334 
335  static ObjectType getType() { return kOutput; }
336  static bool handlesAudio() { return true; }
337 
338 protected:
339 
340  // This prevents the user from needing to implement this method - doing so will do nothing
341 
342  virtual SchedulerInfo schedule(bool newFrame, bool noAdvance) { return SchedulerInfo(); }
343 };
344 
345 // ************************************************************************************** //
346 
347 // FrameLib_Scheduler - Simple class for scheduler type objects
348 
350 {
351 
352 public:
353 
354  FrameLib_Scheduler(FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns = 0, unsigned long nOuts = 0, unsigned long nAudioIns = 0)
355  : FrameLib_DSP(kScheduler, context, owner, info, nIns, nOuts, nAudioIns) {}
356 
357  static ObjectType getType() { return kScheduler; }
358  static bool handlesAudio() { return true; }
359 
360 protected:
361 
362  // This prevents the user from needing to implement this method - doing so will do nothing
363 
364  virtual void process() {}
365 };
366 
367 #endif
ObjectType
Definition: FrameLib_Types.h:24
static bool handlesAudio()
Definition: FrameLib_DSP.h:336
Definition: FrameLib_Types.h:24
Definition: FrameLib_Parameters.h:21
virtual void autoOrderingConnections()
Definition: FrameLib_DSP.cpp:605
FrameLib_Processor(FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns=0, unsigned long nOuts=0)
Definition: FrameLib_DSP.h:286
unsigned long getNumOrderingConnectionObjects()
Definition: FrameLib_DSP.h:168
bool allocateOutputs()
Definition: FrameLib_DSP.cpp:197
FL_FP FrameLib_TimeFormat
Definition: FrameLib_Types.h:20
Definition: FrameLib_Types.h:25
Definition: FrameLib_Context.h:10
FrameLib_TimeFormat getInputFrameTime(unsigned long idx) const
Definition: FrameLib_DSP.h:138
static void zeroVector(double *output, unsigned long size)
Definition: FrameLib_DSP.h:157
static bool handlesAudio()
Definition: FrameLib_DSP.h:290
double * getInput(unsigned long idx, size_t *size)
Definition: FrameLib_DSP.cpp:247
void setParameterInput(unsigned long idx)
Definition: FrameLib_DSP.cpp:161
virtual unsigned long getOutputObjectIdx(unsigned long blockIdx)
Definition: FrameLib_DSP.h:166
virtual FrameType outputType(unsigned long idx) const
Definition: FrameLib_DSP.h:101
Definition: FrameLib_Parameters.h:34
virtual SchedulerInfo schedule(bool newFrame, bool noAdvance)
Definition: FrameLib_DSP.h:320
FrameLib_TimeFormat getBlockEndTime() const
Definition: FrameLib_DSP.h:136
FrameLib_TimeFormat getCurrentTime() const
Definition: FrameLib_DSP.h:134
Definition: FrameLib_DSP.h:17
static bool handlesAudio()
Definition: FrameLib_DSP.h:358
FrameLib_TimeFormat mTimeAdvance
Definition: FrameLib_DSP.h:36
Definition: FrameLib_DSP.h:349
virtual void setFixedInput(unsigned long idx, double *input, unsigned long size)
Definition: FrameLib_DSP.cpp:34
Definition: FrameLib_DSP.h:305
FrameLib_DSP * getOutputObject(unsigned long blockIdx)
Definition: FrameLib_DSP.h:165
static ObjectType getType()
Definition: FrameLib_DSP.h:289
Definition: FrameLib_Object.h:13
FrameLib_TimeFormat getFrameTime() const
Definition: FrameLib_DSP.h:131
ObjectType getType() const
Definition: FrameLib_Object.h:125
FrameLib_DSP * getInputObject(unsigned long blockIdx, unsigned long idx)
Definition: FrameLib_DSP.h:162
Definition: FrameLib_DSP.h:28
FrameLib_TimeFormat getBlockStartTime() const
Definition: FrameLib_DSP.h:135
double * getOutput(unsigned long idx, size_t *size)
Definition: FrameLib_DSP.cpp:264
FrameLib_TimeFormat getInputTime() const
Definition: FrameLib_DSP.h:133
virtual SchedulerInfo schedule(bool newFrame, bool noAdvance)
Definition: FrameLib_DSP.h:296
double mSamplingRate
Definition: FrameLib_DSP.h:235
virtual unsigned long getInputObjectIdx(unsigned long blockIdx, unsigned long idx)
Definition: FrameLib_DSP.h:163
virtual SchedulerInfo schedule(bool newFrame, bool noAdvance)
Definition: FrameLib_DSP.h:342
FrameLib_Parameters mParameters
Definition: FrameLib_DSP.h:240
static ObjectType getType()
Definition: FrameLib_DSP.h:313
Definition: FrameLib_FixedPoint.h:47
void inputMode(unsigned long idx, bool update, bool trigger, bool switchable, FrameType type=kFrameNormal)
Definition: FrameLib_DSP.cpp:151
ManagedPointer< FrameLib_ProcessingQueue, &Global::getProcessingQueue, &Global::releaseProcessingQueue > ProcessingQueue
Definition: FrameLib_Context.h:85
Definition: FrameLib_ProcessingQueue.h:12
void addParameterInput()
Definition: FrameLib_DSP.cpp:169
void requestOutputSize(unsigned long idx, size_t size)
Definition: FrameLib_DSP.h:143
unsigned long getNumInputObjects(unsigned long blockIdx)
Definition: FrameLib_DSP.h:161
SchedulerInfo()
Definition: FrameLib_DSP.h:30
bool mNewFrame
Definition: FrameLib_DSP.h:37
FrameLib_DSP * getOrderingConnectionObject(unsigned long idx)
Definition: FrameLib_DSP.h:169
virtual FrameType inputType(unsigned long idx) const
Definition: FrameLib_DSP.h:100
virtual void process()
Definition: FrameLib_DSP.h:364
void updateTrigger(unsigned long idx, bool trigger)
Definition: FrameLib_DSP.cpp:188
static ObjectType getType()
Definition: FrameLib_DSP.h:357
FrameLib_TimeFormat getInputValidTime(unsigned long idx) const
Definition: FrameLib_DSP.h:139
Definition: FrameLib_Object.h:559
void setIO(unsigned long nIns, unsigned long nOuts, unsigned long nAudioChans=0)
Definition: FrameLib_DSP.cpp:129
FrameLib_AudioOutput(FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns=0, unsigned long nOuts=0, unsigned long nAudioOuts=0)
Definition: FrameLib_DSP.h:332
size_t blockSize(void *ptr)
Definition: FrameLib_Memory.cpp:23
Definition: FrameLib_Parameters.h:153
Definition: FrameLib_DSP.h:281
void outputMode(unsigned long idx, FrameType type)
Definition: FrameLib_DSP.cpp:181
SchedulerInfo(FrameLib_TimeFormat timeAdvance, bool newFrame, bool outputDone)
Definition: FrameLib_DSP.h:33
static bool handlesAudio()
Definition: FrameLib_DSP.h:314
FrameLib_DSP(ObjectType type, FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns, unsigned long nOuts, unsigned long nAudioChans=0)
Definition: FrameLib_DSP.cpp:6
unsigned long mMaxBlockSize
Definition: FrameLib_DSP.h:236
FrameLib_AudioInput(FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns=0, unsigned long nOuts=0, unsigned long nAudioIns=0)
Definition: FrameLib_DSP.h:310
static void copyVector(double *output, double *input, unsigned long size)
Definition: FrameLib_DSP.h:156
Definition: FrameLib_DSP.h:327
static ObjectType getType()
Definition: FrameLib_DSP.h:335
FrameLib_TimeFormat getValidTime() const
Definition: FrameLib_DSP.h:132
FrameLib_Scheduler(FrameLib_Context context, void *owner, FrameLib_Parameters::Info *info, unsigned long nIns=0, unsigned long nOuts=0, unsigned long nAudioIns=0)
Definition: FrameLib_DSP.h:354
void setIO(unsigned long nIns, unsigned long nOuts)
Definition: FrameLib_DSP.h:298
unsigned long getNumAudioIns() const
Definition: FrameLib_Object.h:139
Definition: FrameLib_Types.h:24
bool mOutputDone
Definition: FrameLib_DSP.h:38
FrameType
Definition: FrameLib_Types.h:25
virtual void blockUpdate(double **ins, double **outs, unsigned long blockSize)
Definition: FrameLib_DSP.cpp:56
virtual void reset(double samplingRate, unsigned long maxBlockSize)
Definition: FrameLib_DSP.cpp:72
Definition: FrameLib_Types.h:24
virtual const FrameLib_Parameters * getParameters() const
Definition: FrameLib_DSP.h:98
~FrameLib_DSP()
Definition: FrameLib_DSP.cpp:16
bool isTrigger(unsigned long idx) const
Definition: FrameLib_DSP.h:127